﻿using System;
using System.Threading.Tasks;
using ESRI.ArcGIS.Client.Geometry;

namespace ESRI.ArcGIS.Client.Async
{
    public static partial class DynamicMapServiceLayerExtensions
    {
        /// <summary>
        /// Gets the URL that references the map image.
        /// </summary>
        /// <param name="layer"></param>
        /// <param name="extent">The map extent in map units.</param>
        /// <param name="width">The map width in pixels.</param>
        /// <param name="height">The map height in pixels.</param>
        /// <returns></returns>
        public static Task<string> GetUrlTaskAsync(this DynamicMapServiceLayer layer, Envelope extent,
            int width, int height)
        {
            if (layer == null) throw new ArgumentNullException("layer");
            var tcs = new TaskCompletionSource<string>();
            layer.GetUrl(extent, width, height,
                (completedUrl, completedWidth, completedHeight, completedExtent) => tcs.SetResult(completedUrl));
            return tcs.Task;
        }
    }
}
